//
//  JwAlerter.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-27.
//

#import "JwAlerter.h"

#import "JwMacro.h"
#import "JwCore.h"
#import "JwCate.h"
#import "JwSingle.h"

@implementation JwAlerter

/// 系统弹出框
/// cancelTitle 传入(默认index = 0) 不传(index后推)
/// destructiveTitle 传入(默认index = 1) 不传(index后推)
/// otherTitles 传入(默认index累加)
+ (void)showAlertWithTitle:(nullable NSString *)title
                   message:(nullable NSString *)message
            preferredStyle:(UIAlertControllerStyle)preferredStyle
               cancelTitle:(nullable NSString *)cancelTitle
          destructiveTitle:(nullable NSString *)destructiveTitle
               otherTitles:(nullable NSArray<NSString *> *)otherTitles
                   handler:(void (^)(UIAlertAction *action, NSInteger index))handler {
    
    NSMutableArray *actions = [NSMutableArray array];
    if (cancelTitle) {
        [actions addObject:cancelTitle];
    }
    if (destructiveTitle) {
        [actions addObject:destructiveTitle];
    }
    if (otherTitles) {
        [actions addObjectsFromArray:otherTitles];
    }
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    
    for (NSInteger i = 0; i < actions.count; i++) {
        
        NSString *action = actions[i];
        UIAlertActionStyle style = UIAlertActionStyleDefault;
        if ([action isEqualToString:cancelTitle]) {
            style = UIAlertActionStyleCancel;
        } else if ([action isEqualToString:destructiveTitle]) {
            style = UIAlertActionStyleDestructive;
        }
        
        UIAlertAction *actionV = [UIAlertAction actionWithTitle:action style:style handler:^(UIAlertAction * _Nonnull action) {
            if (handler) {
                handler(action, i);
            }
        }];
        [alertC addAction:actionV];
    }
    UIViewController *vc = [JwScreen frontVC];
    [vc presentViewController:alertC animated:YES completion:nil];
}

+ (void)showAlertWithTitle:(nullable NSString *)title
                   message:(nullable NSString *)message
               otherTitles:(nullable NSArray<NSString *> *)otherTitles
                   handler:(void (^)(UIAlertAction *action, NSInteger index))handler {
    [self showAlertWithTitle:title
                     message:message
              preferredStyle:(UIAlertControllerStyleAlert)
                 cancelTitle:nil
            destructiveTitle:nil
                 otherTitles:otherTitles
                     handler:handler];
}

+ (void)showSheetAlertWithTitle:(nullable NSString *)title
                        message:(nullable NSString *)message
                    cancelTitle:(nullable NSString *)cancelTitle
               destructiveTitle:(nullable NSString *)destructiveTitle
                    otherTitles:(nullable NSArray<NSString *> *)otherTitles
                        handler:(void (^)(UIAlertAction *action, NSInteger index))handler {
    [self showAlertWithTitle:title
                     message:message
              preferredStyle:(UIAlertControllerStyleActionSheet)
                 cancelTitle:cancelTitle
            destructiveTitle:destructiveTitle
                 otherTitles:otherTitles
                     handler:handler];
}


@end

